﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class admin_edLang : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        connection con = new connection();
        SqlConnection cn = con.connect();
        SqlCommand mySqlSelect = new SqlCommand("select * from languages where id = @id", cn);
        mySqlSelect.CommandType = CommandType.Text;
        SqlParameter paramUserName = new SqlParameter("@id", SqlDbType.NVarChar, 2);
        paramUserName.Value = Request.Params["id"];
        mySqlSelect.Parameters.Add(paramUserName);
        try
        {
            SqlDataReader rd = mySqlSelect.ExecuteReader();
            if (rd.HasRows)
            {
                rd.Read();
                curr.Text = rd.GetSqlString(3).Value;
            }
            rd.Close();
            cn.Close();
        }
        catch (Exception ex)
        {
            cn.Close();
            Console.WriteLine("Loi setValueFromDatabaseToForm : " + ex);
        }
    }

    protected void addbt_Click(object sender, EventArgs e)
    {
        lbInfo.Text = "";
        String regex="";
        if (flag.PostedFile.ContentLength > 2097151)
        {
            lbInfo.Visible = true;
            lbInfo.Text = "Tập Tin Hình Ảnh Có Dung Lượng Nhỏ Hơn 2MB!";
        }
        else
        {
            regex = System.IO.Path.GetExtension(flag.PostedFile.FileName.ToLower().ToString());
            String picPath = "~/admin/resource/images/"+ flag.PostedFile.FileName;
            if (regex == ".bmp" || regex == ".jpg" || regex == ".gif" || regex == ".png")
            {
                //Session["filenameSession"] = Session["idProductTemp"].ToString() + regex;
                flag.SaveAs(Server.MapPath(picPath));
            }
            else
            {
                lbInfo.Visible = true;
                lbInfo.Text = "Tập Tin Hình Ảnh Có Phần Mở Rộng là .jpg,.bmp,.gif,.png!";
            }
            connection con = new connection();
            SqlConnection cn = con.connect();
            SqlCommand mySqlSelect = new SqlCommand("update languages set flagUrl = @url, currency= @cur where id = @id", cn);
            if (regex == "")
            {
                mySqlSelect = new SqlCommand("update languages set currency= @cur where id = @id", cn);
            }
            mySqlSelect.CommandType = CommandType.Text;
            SqlParameter paramurl = new SqlParameter("@url", SqlDbType.NVarChar, 150);
            paramurl.Value = flag.PostedFile.FileName;
            SqlParameter paramcur = new SqlParameter("@cur", SqlDbType.NVarChar, 150);
            paramcur.Value = curr.Text;
            SqlParameter id = new SqlParameter("@id", SqlDbType.NVarChar, 2);
            id.Value = Request.Params["id"];
            mySqlSelect.Parameters.Add(paramurl);
            mySqlSelect.Parameters.Add(paramcur);
            mySqlSelect.Parameters.Add(id);
            mySqlSelect.ExecuteNonQuery();
            cn.Close();
            lbInfo.Visible = true;
            lbInfo.Text = "Bạn đã cập nhật thành công";
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?page=qlLang");
    }
}